package com.ruoyi.hotel.controller;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.hotel.domain.HmServiceRequest;
import com.ruoyi.hotel.domain.HmTask;
import com.ruoyi.hotel.domain.HmWorkRate;
import com.ruoyi.hotel.domain.bo.HmWorkRateBo;
import com.ruoyi.hotel.mapper.HmServiceRequestMapper;
import com.ruoyi.hotel.mapper.HmTaskMapper;
import com.ruoyi.hotel.service.IHmTaskService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hotel.domain.vo.HmWorkRateVo;
import com.ruoyi.hotel.service.IHmWorkRateService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 任务评分管理
 *
 * @date 2023-10-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hotel/workRate")
public class HmWorkRateController extends BaseController {

    private final IHmWorkRateService iHmWorkRateService;
    private final IHmTaskService iHmTaskService;

    /**
     * 查询任务评分列表
     */
    @SaCheckPermission("hotel:workRate:list")
    @GetMapping("/list")
    public TableDataInfo<HmWorkRateVo> list(HmWorkRateBo bo, PageQuery pageQuery) {
        return iHmWorkRateService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出任务评分列表
     */
    @SaCheckPermission("hotel:workRate:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HmWorkRateBo bo, HttpServletResponse response) {
        List<HmWorkRateVo> list = iHmWorkRateService.queryList(bo);
        ExcelUtil.exportExcel(list, "【请填写功能名称】", HmWorkRateVo.class, response);
    }

    /**
     * 获取任务评分详细信息
     *
     * @param workRateId 主键
     */
    @SaCheckPermission("hotel:workRate:query")
    @GetMapping("/{workRateId}")
    public R<HmWorkRateVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long workRateId) {
        return R.ok(iHmWorkRateService.queryById(workRateId));
    }

    /**
     * 新增任务评分
     */
    @SaCheckPermission("hotel:workRate:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HmWorkRateBo bo) {
        return toAjax(iHmWorkRateService.insertByBo(bo));
    }

    /**
     * 修改任务评分
     */
    @SaCheckPermission("hotel:workRate:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HmWorkRateBo bo) {
        return toAjax(iHmWorkRateService.updateByBo(bo));
    }

    /**
     * 删除任务评分
     *
     * @param workRateIds 主键串
     */
    @SaCheckPermission("hotel:workRate:remove")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workRateIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] workRateIds) {
        return toAjax(iHmWorkRateService.deleteWithValidByIds(Arrays.asList(workRateIds), true));
    }


    /**
     * 新增评分(同时更新任务)
     * @param hmWorkRate
     */
    @PostMapping("/workRate")
    public void workRate(@RequestBody HmWorkRateBo hmWorkRate){
        Long id=getUserId();

        hmWorkRate.setRater(id);
        hmWorkRate.setIsValid("y");

        iHmWorkRateService.insertByBo(hmWorkRate);

        iHmTaskService.taskDone(hmWorkRate);

    }

    @GetMapping("/complaintList")
    public TableDataInfo<HmWorkRate> getComliants(HmWorkRateBo hmWorkRate) {
        return TableDataInfo.build(iHmWorkRateService.getComplaints(hmWorkRate));
    }

    /**
     * 投诉
     */
    @RepeatSubmit()
    @PutMapping("/complaint")
    public R<Void> Complaint(@RequestBody HmWorkRateBo hmWorkRate){
        if(hmWorkRate.getIsValid().equals("y")){
            iHmWorkRateService.updateByBo(hmWorkRate);
            return toAjax(true);
        }
        else{
            return toAjax(false);
        }
    }

    /**
     * 我的评分
     */
    @GetMapping("/myRate")
    public TableDataInfo<HmWorkRateVo> myRate(HmWorkRateBo bo, PageQuery pageQuery) {
        bo.setWorkerId(getUserId());
        return iHmWorkRateService.queryPageList(bo, pageQuery);
    }


    /**
     * 投诉确实，将评分无效化
     */
    @RepeatSubmit()
    @PostMapping("/confirmComplaint")
    public void CancelRate(@RequestBody HmWorkRateBo hmWorkRate){
        hmWorkRate.setIsValid("n");
        iHmWorkRateService.updateByBo(hmWorkRate);

        HmWorkRateBo warn = new HmWorkRateBo();
        warn.setRater(getUserId());
        warn.setWorkerId(hmWorkRate.getRater());
        warn.setRate(0L);
        warn.setIsValid("y");

        iHmWorkRateService.insertByBo(warn);
    }



}
